package com.yanq.liancar.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.ServiceDetailsAdapter;
import com.yanq.liancar.adapter.SmallBGoodsListAdapter;
import com.yanq.liancar.bean.ServiceDetailsBean;
import com.yanq.liancar.bean.ShopEvaluateBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BuyStandardCarWashActivity extends BaseToolbarActivity {
    @BindView(R.id.action_bar)
    AppBarLayout actionBar;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_servicename)
    TextView tvServicename;
    @BindView(R.id.tv_serviceprice)
    TextView tvServiceprice;
    @BindView(R.id.tv_serviceprice1)
    TextView tvServiceprice1;
    @BindView(R.id.tv_mna)
    TextView tvMna;
    @BindView(R.id.tv_ServiceIntroduction)
    TextView tvServiceIntroduction;
    @BindView(R.id.service_imgList)
    RecyclerView serviceImgList;
    @BindView(R.id.tv_buy_now)
    TextView tvBuyNow;
    private int type;
    private ServiceDetailsAdapter adapter;
    private List<ServiceDetailsBean.DataBean.FilesBean>  mList=new ArrayList<>();
    private String statu , url,id;  //精致洗车小分类   店铺id
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buystandardcarwash);
        ButterKnife.bind(this);
        Utils.setStatusBar(this, true, false);

        Bundle bundle = getIntent().getExtras();

        type = bundle.getInt("type",0);
        id=bundle.getString("id");
        if (type==80||type==90) {
            mTvTitle.setText("精致洗车");
            statu=bundle.getString("statu");
        } else if (type==2) {
            mTvTitle.setText("标准洗车");
        }
//        else if (type==3){
//            mTvTitle.setText("美容养护");
//            id=bundle.getInt("id",0);
//        }else if (type==4){
//            mTvTitle.setText("保养维修");
//            id=bundle.getInt("id",0);
//        }else if (type==5){
//            mTvTitle.setText("钣金喷漆");
//            id=bundle.getInt("id",0);
//        }else if (type==6){
//            mTvTitle.setText("代办服务");
//            id=bundle.getInt("id",0);
//        }else if (type==7){
//            mTvTitle.setText("全车打蜡");
//            id=bundle.getInt("id",0);
//        }

        if (type==80||type==90){
            url="llc/fineCarServerController/queryFineCarWashServerDetail";
        }else {
            url="llc/showCarServiceInfo";
        }
        initRecyclerView();
        initData();
    }


    @OnClick(R.id.tv_buy_now)
    public void onViewClicked() {
        Bundle bundle=new Bundle();
        if (type==80||type==90) {
            bundle.putString("title","精致洗车");
            bundle.putString("statu",statu);
            bundle.putString("id",id);
            bundle.putInt("serviceType",type);
        } else if (type==2) {
            bundle.putString("title", "标准洗车");
            bundle.putString("id", id);
            bundle.putInt("serviceType",2);
        }
//        }else if (type==3){
//            bundle.putString("title","美容养护");
//            bundle.putString("id",id+"");
//        }else if (type==4){
//            bundle.putString("title","保养维修");
//            bundle.putString("id",id+"");
//        }else if (type==5){
//            bundle.putString("title","钣金喷漆");
//            bundle.putString("id",id+"");
//        }else if (type==6){
//            bundle.putString("title","代办服务");
//            bundle.putString("id",id+"");
//        }else if (type==7){
//            bundle.putString("title","全车打蜡");
//            bundle.putString("id",id+"");
//        }

        RxActivityTool.skipActivity(this, PaymentActivity.class,bundle);
    }

    private void initRecyclerView() {
        serviceImgList.setNestedScrollingEnabled(false);
        LinearLayoutManager manager=new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        serviceImgList.setLayoutManager(manager);
        adapter=new ServiceDetailsAdapter(R.layout.item_servicedetails,mList);
        serviceImgList.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //RxActivityTool.skipActivity(mContext,SmallBGoodsDetailsActivity.class);
            }
        });
    }

    private void initData() {
//        LogUtils.e("sid=====",id);
        OkGo.<String>post(Config.s +url )
                .tag(this)
                .params("sid", id)
                .params("type", type)
                .params("token", UserBean.getcToken(this))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        ServiceDetailsBean serviceDetailsBean = gson.fromJson(response.body(), ServiceDetailsBean.class);
                        if (serviceDetailsBean.isIsSuccess()) {
                            if (serviceDetailsBean.getData().getServiceType()==80||serviceDetailsBean.getData().getServiceType()==90){
                                tvType.setText("精致洗车");
                            }else if (serviceDetailsBean.getData().getServiceType()==2){
                                tvType.setText("标准洗车");
                            }
//                            else if (serviceDetailsBean.getData().getServiceType()==3){
//                                tvType.setText("美容养护");
//                            }else if (serviceDetailsBean.getData().getServiceType()==4){
//                                tvType.setText("到店保养");
//                            }else if (serviceDetailsBean.getData().getServiceType()==5){
//                                tvType.setText("钣金喷漆");
//                            }else if (serviceDetailsBean.getData().getServiceType()==6){
//                                tvType.setText("代办服务");
//                            }else {
//
//                            }
                            tvServicename.setText(serviceDetailsBean.getData().getServiceName());
                            tvServiceprice.setText(serviceDetailsBean.getData().getServiceyuanprice()+"");
                            tvServiceprice1.setText(serviceDetailsBean.getData().getServicePrice()+"");
                            tvMna.setText(serviceDetailsBean.getData().getServiceRemark());
                            tvServiceIntroduction.setText(serviceDetailsBean.getData().getServiceInfo());
                            mList.clear();
                            if (serviceDetailsBean.getData().getFiles().size()>3){
                                for (int i = 0; i <3 ; i++) {
                                    mList.add(serviceDetailsBean.getData().getFiles().get(i));
                                }
                            }else {
                                for (int i = 0; i <serviceDetailsBean.getData().getFiles().size(); i++) {
                                    mList.add(serviceDetailsBean.getData().getFiles().get(i));
                                }
                            }
                            LogUtils.e("111111111111111",mList.size()+"");
                            adapter.setNewData(mList);
                        } else {
                            RxToast.error(serviceDetailsBean.getOutputMessage());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        RxToast.error("服务器连接失败");
                    }
                });
    }
}
